﻿using System.Drawing;
using System.Drawing.Drawing2D;
using GraphDrawing2.Canvas;
using GraphDrawing2.Core;

namespace GraphDrawing2.V2.Graph.Model
{
    public class ConnectorView : IInitalizable<ConnectorModel>
    {
        readonly GraphicsPath interior;
        readonly Appearance standard;
        readonly Appearance hovered;
        readonly Appearance selected;

        ConnectorModel model;

        public ConnectorView()
        {
            interior = new GraphicsPath();
            standard = new Appearance();
            hovered = new Appearance();
            selected = new Appearance();
            hovered.Border.Width = 2f;
        }

        public void Initialize(ConnectorModel model)
        {
            this.model = model;
            interior.AddEllipse(model.Bounds);
        }

        public bool Contains(PointF p)
        {
            return interior.IsVisible(p);
        }

        public void Render(DrawingContext dc)
        {
            var g = dc.graphics;
            using (g.DrawLocal())
            {
                g.TranslateTransform(model.Position.X, model.Position.Y);
                var appearance = GetAppearance();
                using (var brush = appearance.CreateBrush())
                    g.FillPath(brush, interior);
                using (var pen = appearance.Border.CreatePen())
                    g.DrawPath(pen, interior);
            }
        }

        Appearance GetAppearance()
        {
            if (model.IsSelected())
                return selected;
            if (model.IsHovered())
                return hovered;
            return standard;
        }
    }
}